
<template>
  <div>
    <el-table
        border
        :data="authConfigs"
        tooltip-effect="dark"
        height="290px"
        size="small"
        ref="singleTable"
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%">

      <el-table-column
          prop="authRes"
          label="权限资源"
          width="250">
        <template slot-scope="scope">{{ scope.row.authRes }}</template>
      </el-table-column>
      <el-table-column
          prop="allowFlag"
          label="是否允许"
          width="200">
        <template slot-scope="scope">{{ scope.row.allowFlag ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column
          prop="refuseFlag"
          label="是否拒绝"
          width="300">
        <template slot-scope="scope">{{ scope.row.refuseFlag ? '是' : '否' }}</template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px;">
      <el-button type="primary"  size="small" @click="add">添加</el-button>
      <el-button type="primary"  size="small" @click="update">修改</el-button>
      <el-button type="primary"  size="small" @click="remove">删除</el-button>
    </div>
    <AddAuth ref="addAuth"  @success="addAuthRecord"></AddAuth>
    <UpdateAuth ref="updateAuth"  @success="updateAuthRecord"></UpdateAuth>
  </div>

</template>
<script>
import AddAuth from '../auth/AddAuth.vue';
import UpdateAuth from '../auth/UpdateAuth.vue';
import { utils } from '../../utils/common.js'
export default {
  props:[
      "parentNode",
      "process"
  ],
  components:{
    AddAuth,
    UpdateAuth
  },
  data(){
    return {
      node:{},
      labelPosition:'right',
      authConfigs:[
      ],
      nowAuthConfig:{},
    }
  },
  created(){
  },
  computed:{
  },
  methods:{
    showPro(node){
      this.node = node;
      let config = node.authConfigs;
      this.authConfigs = config == null ? [] : config;
      this.authConfigs.forEach(config=>{
        config.cid = config.id;
      })
    },
    handleCurrentChange(row){
      this.nowAuthConfig = row;
    },
    add(){
      this.$refs["addAuth"].showDialog(this.node);
    },
    update(){
      if(this.nowAuthConfig == null){
        return;
      }
      let authConfig = this.authConfigs[this.authConfigs.findIndex(config=>config.cid === this.nowAuthConfig.cid)]
      this.$refs["updateAuth"].showDialog(authConfig,this.node);
    },
    remove(){
      if(this.nowAuthConfig == null){
        return;
      }
      let index = this.authConfigs.findIndex(config=>config.cid === this.nowAuthConfig.cid);
      this.authConfigs.splice(index,1);
    },
    addAuthRecord(row){
      this.authConfigs.push(row);
      this.setAuthConfigs();
    },
    updateAuthRecord(row){
      let index = this.authConfigs.findIndex(config=>config.cid === row.cid);
      this.authConfigs[index] = row;
      this.authConfigs = utils.getCloneObj(this.authConfigs);
      this.setAuthConfigs();
    },
    setAuthConfigs() {
      if (this.node.type === 'process') {
        this.process.authConfigs = this.authConfigs;
      } else {
        this.parentNode.authConfigs = this.authConfigs;
      }
    },
  }
}
</script>

<style scoped lang="scss">

</style>